<?php
declare (strict_types = 1);

namespace app\admin\controller;

use app\admin\middleware\Auth;
use app\BaseController;
use app\common\model\User\User as UserModel;
use think\facade\View;
use think\facade\Db;
use app\common\model\Post\Cate as CateModel;
use app\common\model\Post\Post as PostModel;
use app\common\model\User\Record as RecordModel;

class Analysis extends BaseController
{

    protected $middleware = [
        Auth::class
    ];

    // 用户量统计
    public function user()
    {
        $nav_title = [
            'nav_title' => '数据统计',
            'title' => '用户量统计'
        ];

        if($this->request->isAjax())
        {
            $action = $this->request->param('action', '', 'trim');

            //饼图
            if($action == 'pie')
            {
                //按性别
                $sex1 = UserModel::where(['sex' => '1'])->count();
                $sex2 = UserModel::where(['sex' => '2'])->count();
                $sex0 = UserModel::where(['sex' => '0'])->count();

                //按认证
                $auth1 = UserModel::where(['auth' => '1'])->count();
                $auth0 = UserModel::where(['auth' => '0'])->count();

                //组装数据
                $data = [
                    'sex' => [$sex1,$sex2,$sex0],
                    'auth' => [$auth1,$auth0]
                ];

                $this->success('用户分析', null, $data);
                exit;
            }


        }

        return View::fetch('',[
            'nav_title' => $nav_title
        ]);
    }

    // 帖子分析
    public function post()
    {
        $nav_title = [
            'nav_title' => '数据统计',
            'title' => '问答统计'
        ];

        if($this->request->isAjax())
        {
            $action = $this->request->param('action', '', 'trim');

            //线性图数据
            if($action == 'line')
            {
                $catelist = CateModel::field('id,name')->select();

                //提取名字
                $name = array_column($catelist->toArray(), 'name');

                //提取id数据
                $ids = array_column($catelist->toArray(), 'id');

                //已采纳 /未采纳
                $Accept = $NoAccept = [];

                foreach($ids as $cateid)
                {
                    //已采纳
                    $Accept[] = PostModel::where([['cateid','=', $cateid], ['accept','EXP',Db::raw('IS NOT NULL')]])->count();

                    //未采纳
                    $NoAccept[] = PostModel::where([['cateid','=', $cateid], ['accept','EXP',Db::raw('IS NULL')]])->count();
                }

                //组装数据
                $data = [
                    'name'=> $name,
                    'Accept' => $Accept,
                    'NoAccept' => $NoAccept
                ];

                $this->success('返回数据成功', null, $data);
                exit;
            }


        }

        return View::fetch('',[
            'nav_title' => $nav_title
        ]);
    }

    // 消费记录统计
    public function record()
    {
        $nav_title = [
            'nav_title' => '数据统计',
            'title' => '消费记录统计'
        ];

        if($this->request->isAjax())
        {
            $action = $this->request->param('action', '', 'trim');

            //饼图
            if($action == 'record')
            {
                //获取当前年份
                $year = date("Y", time());

                $TimeList = [];

                for($i=1;$i<=12;$i++)
                {
                    $start = "$year-$i-01";
                    $end = date("Y-m-d", strtotime("$start +1 month -1 day"));
                    $TimeList[] = [$start, $end];
                }

                $state1 = ['发布'];
                $state2 = ['采纳'];
                $state3 = ['充值'];
                $state4 = ['签到'];
                $state5 = ['商品订单'];
                $state6 = ['预约订单'];
                $state7 = ['退款'];
                foreach($TimeList as $item)
                {
                    $state1[] = RecordModel::whereTime('createtime', 'between', $item)->where(['state' => '1'])->count();
                    $state2[] = RecordModel::whereTime('createtime', 'between', $item)->where(['state' => '2'])->count();
                    $state3[] = RecordModel::whereTime('createtime', 'between', $item)->where(['state' => '3'])->count();
                    $state4[] = RecordModel::whereTime('createtime', 'between', $item)->where(['state' => '4'])->count();
                    $state5[] = RecordModel::whereTime('createtime', 'between', $item)->where(['state' => '5'])->count();
                    $state6[] = RecordModel::whereTime('createtime', 'between', $item)->where(['state' => '6'])->count();
                    $state7[] = RecordModel::whereTime('createtime', 'between', $item)->where(['state' => '-1'])->count();
                }

                //X轴
                $month = ['record'];
                for($i=1;$i<=12;$i++)
                {
                    $month[] = "{$i}月份";
                }

                //组装数据
                $data = [
                    $month,
                    $state1,
                    $state2,
                    $state3,
                    $state4,
                    $state5,
                    $state6,
                    $state7,
                ];

                $this->success('消费记录统计', null, $data);
                exit;
            }


        }
        return View::fetch('',[
            'nav_title' => $nav_title
        ]);
    }
}
